package com.tea.room.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tea.room.model.Return;
import com.tea.room.model.UserInfo;
import com.tea.room.service.wechat.MenuService;
import com.tea.room.service.wechat.PayService;
import com.tea.room.service.wechat.WeChatService;

/**
 * 微信支付
 * @author Administrator
 *
 */
@RestController("weChatPayController")
public class WeChatPayController {

    @Autowired
    private PayService payService;
    
    @Autowired
    private WeChatService weChatService;
    
    @Autowired
    private MenuService menuService;


    /**
     * 获取支付下单接口
     */
    @ResponseBody
    @RequestMapping("/payInfo")
    public Return getPayInfo(HttpServletRequest request, HttpServletResponse response) {
        Return rn = new Return();
        String out_trade_no = request.getParameter("out_trade_no");
        String price = request.getParameter("price");
        String openid = request.getParameter("openid");
        rn = payService.getPayParams(out_trade_no, price, openid);
        return rn;
    }
    
    /**
     * 查询订单接口
     */
    @ResponseBody
    @RequestMapping("/queryOrder")
    public Return queryOrder(HttpServletRequest request, HttpServletResponse response) {
        Return rn = new Return();
        rn = payService.queryOrder(request);
        return rn;
    }
    
    /**
     * 关闭订单接口
     */
    @ResponseBody
    @RequestMapping("/closeOrder")
    public Return closeOrder(HttpServletRequest request, HttpServletResponse response) {
        Return rn = new Return();
        rn = payService.closeOrder(request);
        return rn;
    }
    
    /**
     * 退款接口
     * @throws Exception 
     */
    @ResponseBody
    @RequestMapping("/refund")
    public Return refund(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Return rn = new Return();
        String out_trade_no = request.getParameter("out_trade_no");
        String total_fee = request.getParameter("total_fee");
        rn = payService.payRefund(out_trade_no, total_fee);
        return rn;
    }
    
    /**
     * 退款结果查询接口
     * @throws Exception 
     */
    @ResponseBody
    @RequestMapping("/refundQuery")
    public Return refundQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Return rn = new Return();
        String out_trade_no = request.getParameter("out_trade_no");
        rn = payService.queryRefund(out_trade_no);
        return rn;
    }

    /**
     * 支付结果通知接口
     * @throws IOException 
     */
    @ResponseBody
    @RequestMapping("/weChatNotifyUrl")
    public void callBackPay(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	String xml = payService.weChatPayCallBack(request);
        PrintWriter out = response.getWriter();
        out.print(xml);
        out.close();
    }
    
    /**
     * 退款通知接口
     */
    @ResponseBody
    @RequestMapping("/refundCallback")
    public Return refundCallback(HttpServletRequest request, HttpServletResponse response) {
        
        return null;
    }
    
    /**
     * 自定义菜单创建接口
     * 传入参数示例：https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
     */
    @ResponseBody
    @RequestMapping("/addDiyMenu")
    public Return addDiyMenu(HttpServletRequest request, HttpServletResponse response) {
        Return rn = menuService.addMenu(request);
        return rn;
    }
    
    /**
     * 查询菜单接口
     */
    @ResponseBody
    @RequestMapping("/queryMenu")
    public Return queryDiyMenu(HttpServletRequest request, HttpServletResponse response) {
        Return rn = menuService.queryMenu(request);
        return rn;
    }
    
    /**
     * 删除菜单接口
     */
    @ResponseBody
    @RequestMapping("/deleteMenu")
    public Return deleteDiyMenu(HttpServletRequest request, HttpServletResponse response) {
        Return rn = menuService.removeMenu(request);
        return rn;
    }
    
    /**
     * 查询用户信息
     */
    @ResponseBody
    @RequestMapping("/queryUserInfo")
    public UserInfo queryUser(HttpServletRequest request, HttpServletResponse response) {
        String openid = request.getParameter("openid");
        UserInfo info = weChatService.queryUserInfo(openid);
        return info;
    }
    
    /**
     * 查询用户信息
     */
    @ResponseBody
    @RequestMapping("/addUserInfo")
    public Return addUser(HttpServletRequest request, HttpServletResponse response) {
        String openid = request.getParameter("openid");
        Return return1 = weChatService.addUserInfo(openid);
        return return1;
    }
    
    /**
     * 查询openid
     */
    @ResponseBody
    @RequestMapping("/getOpenid")
    public Return getOpenid(HttpServletRequest request, HttpServletResponse response) {
    	String code = request.getParameter("code");
    	Return rn = weChatService.queryOpenid(code);
        return rn;
    }
}
